package com.guchenbo.hadoop.demo;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;

import java.io.IOException;

/**
 * @author guchenbo
 * @date 2024/8/26
 */
public class KerberosUtils {
    public static final String HADOOP_SECURITY_AUTHENTICATION = "hadoop.security.authentication";
    public static final String HADOOP_SECURITY_AUTHENTICATION_VALUE = "kerberos";

    public static void main(String[] args) throws IOException {
//        Configuration configuration = new Configuration();
        String principal = args[0];
                        //"simba-os";
        String keytab = args[1];
                        //"/Users/guchenbo/opt/etc/kerberos/dev02/keytab/simba-os.keytab";
        String krb5Conf = args[2];
                        //"/etc/krb5.conf";
        System.setProperty("java.security.krb5.conf", krb5Conf);

        Configuration conf = new Configuration();
        conf.set(HADOOP_SECURITY_AUTHENTICATION, HADOOP_SECURITY_AUTHENTICATION_VALUE);
        UserGroupInformation.setConfiguration(conf);
        UserGroupInformation.loginUserFromKeytab(principal, keytab);
        System.out.println(UserGroupInformation.getLoginUser());
    }
}
